package com.company.cloud.web.controller;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.company.cloud.web.models.Person;
import com.company.cloud.web.models.SitMapModel;
import com.company.cloud.web.render.Render;
import com.company.cloud.web.render.RenderFactory;
import com.company.cloud.web.render.RenderManager;
import com.company.cloud.web.render.kit.HttpContextUtils;
import com.company.cloud.web.service.WebArtService;
import com.company.cloud.web.service.WebIndexService;
import com.company.cloud.web.service.WebPrdService;
import com.jfinal.kit.Kv;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.*;

/**
 * 艺术品
 * */
@Controller
@RequestMapping("/works")
public class ProductController {

    @Autowired
    private WebPrdService webPrdService;

    @Autowired
    private WebIndexService webIndexService;

    @Autowired
    private WebArtService webArtService;

    /**
     * 商品详细信息
     * */
    @RequestMapping("/detail")
    public String detail(Model model,String productNo){
        JSONObject product= webPrdService.info(productNo);
        Map<String, Object> indexMap= webIndexService.getPageIndexData();
        model.addAttribute("category",indexMap.get("category"));
        model.addAttribute("data",product);
        Map<String, Object> menusMap= webArtService.staticPageTree("top");
        model.addAttribute("menu",menusMap);

        Map<String, Object> footer= webArtService.staticPageTree("bottom");
        model.addAttribute("footer",footer);
        model.addAttribute("friendLink",indexMap.get("friendLink"));
        model.addAttribute("copyright",indexMap.get("copyright"));
        Map<String,String> params  = new HashMap<String,String>();
        params.put("num","8");

        Map<String, Object> pMap= webPrdService.getPrdRecommendList(params);
        model.addAttribute("pMap",pMap);
        return "works/detail";
    }


    /**
     * 商品详细信息
     * */
    @RequestMapping("/detail56")
    public String detail56(Model model,String productNo,@CookieValue(value="org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE" ,defaultValue = "zh-CN") String i18n){
        JSONObject product= webPrdService.info(productNo);
        Map<String, Object> indexMap= webIndexService.getPageIndexData();
        model.addAttribute("category",indexMap.get("category"));
        model.addAttribute("data",product);

        Map<String, Object> menusMap= webArtService.staticPageTree("top");
        model.addAttribute("menu",menusMap);

        model.addAttribute("i18n",i18n);
        Map<String, Object> footer= webArtService.staticPageTree("bottom");
        model.addAttribute("footer",footer);
        model.addAttribute("friendLink",indexMap.get("friendLink"));
        model.addAttribute("copyright",indexMap.get("copyright"));
        Map<String,String> params  = new HashMap<String,String>();
        params.put("num","8");

        Map<String, Object> pMap= webPrdService.getPrdRecommendList(params);
        model.addAttribute("pMap",pMap);
        return "works/detail56";
    }

    /**
     * 商品查询列表
     * */
    @RequestMapping("/index")
    public String query(Model model,@RequestParam Map<String,String> params,@CookieValue(value="org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE" ,defaultValue = "zh-CN") String i18n){
        params.put("rows","33");
        JSONObject object = webPrdService.queryPrd(params);
        model.addAttribute("data",object);

        model.addAttribute("i18n",i18n);
        Map<String, Object> footer= webArtService.staticPageTree("bottom");
        model.addAttribute("footer",footer);

        model.addAttribute("params",params);
        Map<String, Object> indexMap= webIndexService.getPageIndexData();
        model.addAttribute("category",indexMap.get("category"));
        Map<String, Object> menusMap= webArtService.staticPageTree("top");
        model.addAttribute("menu",menusMap);

        model.addAttribute("friendLink",indexMap.get("friendLink"));
        model.addAttribute("copyright",indexMap.get("copyright"));
        JSONObject tree =webPrdService.categoryTree();
        model.addAttribute("tree",tree);
        return "works/index";
    }


    /**
     * 商品分类
     * */
    @RequestMapping("/categoryTree")
    public String categoryTree(Model model,@CookieValue(value="org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE" ,defaultValue = "zh-CN") String i18n){
        JSONObject tree =webPrdService.categoryTree();
        model.addAttribute("tree",tree);
        return "works/categoryTree";
    }


//    @RequestMapping("/test")
//    public String test(Model model,@RequestParam Map<String,Object> params2){
//        Map<String,Object> params=new HashMap<>();
//        params.put("categoryId",model.getAttribute("categoryId"));
//        params.put("name",model.getAttribute("name"));
//        params.put("page",model.getAttribute("page"));
//        params.put("rows",model.getAttribute("rows"));
//        params.put("type",model.getAttribute("type"));
//        params.put("twoType",model.getAttribute("twoType"));
//        params.put("tagId",model.getAttribute("tagId"));
//
//        params.put("A","AAAA");
//        params.put("B",91.6);
//        params.put("C", BigDecimal.ZERO);
//        params.put("D","D");
//        model.addAttribute("params",params);
//
//        return "works/test";
//    }

    @RequestMapping("/test2")
    public void test2(){
        RenderFactory factory= new RenderFactory();
        Engine engine=Engine.use();
        engine.setDevMode(true);
        engine.setToClassPathSourceFactory();
        engine.setBaseTemplatePath("/view/");

        String lastDate=DateUtil.format(new Date(),"yyyy-MM-dd");

        Map<String,List<SitMapModel>> data=new HashMap<>();

        List<SitMapModel> prdList=new ArrayList<>();
        prdList.add(new SitMapModel("1111",lastDate,"www"));
        prdList.add(new SitMapModel("2222",lastDate,"www"));
        data.put("prd",prdList);

        Template template = engine.getTemplate("baidu_sitmap.html");
        String result = template.renderToString(data);

        Render render=factory.getXmlRender(result);
        render.setContext(HttpContextUtils.getRequest(),HttpContextUtils.getResponse());
        render.render();
    }

}
